#include <iostream>
#include <string>
#include <expected>
#include <vector>
#include <cmath>

// 使用std::expected处理可能的错误
std::expected<double, std::string> safe_divide(double a, double b) {
  if (b == 0) {
    return std::unexpected("除数不能为零");
  }
  return a / b;
}

// 使用std::expected处理可能的错误
std::expected<int, std::string> parse_number(const std::string& str) {
  try {
    return std::stoi(str);
  } catch (const std::exception& e) {
    return std::unexpected("无法解析数字: " + std::string(e.what()));
  }
}

// 使用std::expected处理可能的错误
std::expected<double, std::string> calculate_square_root(double x) {
  if (x < 0) {
    return std::unexpected("不能计算负数的平方根");
  }
  return std::sqrt(x);
}

// 使用std::expected处理可能的错误
std::expected<std::vector<int>, std::string> create_range(int start, int end) {
  if (start > end) {
    return std::unexpected("起始值大于结束值");
  }
  
  std::vector<int> result;
  result.reserve(end - start + 1);
  
  for (int i = start; i <= end; ++i) {
    result.push_back(i);
  }
  
  return result;
}

// 使用std::expected处理可能的错误
std::expected<std::string, std::string> format_name(const std::string& first, 
                                                   const std::string& last) {
  if (first.empty() || last.empty()) {
    return std::unexpected("名字和姓氏不能为空");
  }
  
  return first + " " + last;
}

// 演示std::expected的使用
void demonstrate_expected() {
  std::cout << "\n=== 演示 std::expected ===\n";
  
  // 1. 基本用法
  std::cout << "\n1. 基本用法:\n";
  auto result1 = safe_divide(10.0, 2.0);
  if (result1) {
    std::cout << "10 / 2 = " << *result1 << "\n";
  } else {
    std::cout << "错误: " << result1.error() << "\n";
  }
  
  auto result2 = safe_divide(10.0, 0.0);
  if (result2) {
    std::cout << "10 / 0 = " << *result2 << "\n";
  } else {
    std::cout << "错误: " << result2.error() << "\n";
  }
  
  // 2. 错误传播
  std::cout << "\n2. 错误传播:\n";
  auto result3 = parse_number("123");
  if (result3) {
    std::cout << "解析成功: " << *result3 << "\n";
  } else {
    std::cout << "解析失败: " << result3.error() << "\n";
  }
  
  auto result4 = parse_number("abc");
  if (result4) {
    std::cout << "解析成功: " << *result4 << "\n";
  } else {
    std::cout << "解析失败: " << result4.error() << "\n";
  }
  
  // 3. 链式操作
  std::cout << "\n3. 链式操作:\n";
  auto result5 = calculate_square_root(16.0);
  if (result5) {
    std::cout << "sqrt(16) = " << *result5 << "\n";
  } else {
    std::cout << "计算失败: " << result5.error() << "\n";
  }
  
  auto result6 = calculate_square_root(-16.0);
  if (result6) {
    std::cout << "sqrt(-16) = " << *result6 << "\n";
  } else {
    std::cout << "计算失败: " << result6.error() << "\n";
  }
  
  // 4. 复杂操作
  std::cout << "\n4. 复杂操作:\n";
  auto result7 = create_range(1, 5);
  if (result7) {
    std::cout << "范围 [1,5]: ";
    for (int x : *result7) {
      std::cout << x << " ";
    }
    std::cout << "\n";
  } else {
    std::cout << "创建范围失败: " << result7.error() << "\n";
  }
  
  auto result8 = create_range(5, 1);
  if (result8) {
    std::cout << "范围 [5,1]: ";
    for (int x : *result8) {
      std::cout << x << " ";
    }
    std::cout << "\n";
  } else {
    std::cout << "创建范围失败: " << result8.error() << "\n";
  }
  
  // 5. 字符串操作
  std::cout << "\n5. 字符串操作:\n";
  auto result9 = format_name("John", "Doe");
  if (result9) {
    std::cout << "格式化名字: " << *result9 << "\n";
  } else {
    std::cout << "格式化失败: " << result9.error() << "\n";
  }
  
  auto result10 = format_name("", "Doe");
  if (result10) {
    std::cout << "格式化名字: " << *result10 << "\n";
  } else {
    std::cout << "格式化失败: " << result10.error() << "\n";
  }
  
  // 6. 与异常处理的比较
  std::cout << "\n6. 与异常处理的比较:\n";
  std::cout << "std::expected 优点:\n";
  std::cout << "- 错误处理更加显式和可控\n";
  std::cout << "- 可以返回多个错误值\n";
  std::cout << "- 性能开销更小\n";
  std::cout << "- 可以与结构化绑定配合使用\n";
  
  // 7. 实际应用场景
  std::cout << "\n7. 实际应用场景:\n";
  std::cout << "- 文件操作\n";
  std::cout << "- 网络请求\n";
  std::cout << "- 数据库操作\n";
  std::cout << "- 配置解析\n";
  std::cout << "- API调用\n";
} 